import RNFetchBlob from 'react-native-fetch-blob';

const uploadFile = (url, body, uploadProgress, successCallBack, failCallBack) => {
  return RNFetchBlob
    .config({ indicator: true })
    .fetch('POST', url, {
      'Content-Type': 'multipart/form-data',
    }, body)
    .progress((received, total) => {
      const perent = received / total;
      // 搜索进度打印
      uploadProgress(perent);
    })
    .then((response) => {
      if (response.respInfo.status === 200) {
        return response.json();
      } else {
        return failCallBack(response);
      }
    })
    .then((response) => {
      successCallBack(response);
    })
    .catch((error) => {
      failCallBack(error);
    });
};

export default uploadFile;
